const util = require('../../utils/util.js')
const getApps = getApp().globalData
const Monitor = require('../../utils/monitor.js').Monitor
Page(Monitor.hookApp({
  data: {
    pid: 0,
    lists: [],
    imgUrl: getApps.imgUrl,
    imgUrls: getApps.imgUrls,
    current: 1,
    iamgeArr: [1, 2.3, 2],
    showSpecification: false,
    timeList: [],
    skuTypeList: [],
    showDistribution: false,
    right: '038b5201805161046447142.png',
    rightImage: "wxshop2854jh5sdf45左边.svg",
    groupList: false
  },
  onLoad(options) {
    console.log(options)
    this.setData({
      isShare: options.isShare == 'true' ? true : false,
      pid: options.pid || 0,
      activity_id: options.activity_id,
      goods_id: options.goods_id
    })
    this.goodsInfo()
  },

  onShow() {
    this.addBrows(2, this.data.activity_id, this.data.goods_id)
  },
  groupList() {
    this.setData({
      groupList: !this.data.groupList
    })
  },
  addBrows(type, type_id, goods_id) { // 增加商品浏览记录
    util.API('sp/v1.Assemble/addBrows', {
      type, //1普通 2 拼团 3折扣
      type_id,//活动id
      goods_id//活动id
    })
    // this.addBrows(1, 0, this.data.goods_id)
  },
  bindGetUserInfoCon(e) {
    console.log(e.currentTarget.dataset.item, 'ptItem')
    let ev = e.currentTarget.dataset,
      that = this;
    this.setData({
      ptItem: ev.item
    })
    if (e.detail.userInfo) {
      this.setData({
        market_type: parseInt(ev.market_type),
        pid: ev.pid,
        showSpecification: !this.data.showSpecification,
        groupList: false
      })
      util.API('wechat/authorize/updateapp', {
        nick_name: e.detail.userInfo.nickName,
        cover: e.detail.userInfo.avatarUrl
      })
    } else {

    }

  },
  previewImage(e) {
    let current = e.target.dataset.src,
      that = this;
    wx.previewImage({
      current: that.data.imgUrls + current, // 当前显示图片的http链接
      urls: getimg()
    })

    function getimg() {
      var arrimage = []
      that.data.data.goods_cover.map((item, index) => {
        arrimage.push(that.data.imgUrls + item)
      })
      return arrimage
    }
  },
  showDistribution() {
    this.setData({
      showDistribution: !this.data.showDistribution
    })
  },
  getShopList() {
    let that = this;
    util.API('spwx/v1.Shop/continueLookLook', {
      goods_id: that.data.goods_id
    }).then(({
      code,
      data
    }) => {
      if (code == 200) {
        that.setData({
          lists: res.data.list,
        })
      }

    })
  },
  goodsInfo() {
    let that = this,
      {
        activity_id,
        goods_id,
        up_user,
        down_user,
        isShare,
        pid
      } = this.data;
    util.API('pyapi/assemble_info', {
      activity_id,
      pid,
      goods_id,
      up_user,
      down_user
    }, 'application/x-www-form-urlencoded').then(({
      data,
      assemble_info,
      assemble_people,
      defaultIndex,
      distribution_name,
      msg,
      need_nums,
      sku,
      code
    }) => {
      if (code == 200) {
        if (isShare) {
          this.setData({
            ptItem: {
              d_time: 0
            }
          })
        }
        data.distribution_name = JSON.stringify(distribution_name)
        data.defaultIndex = defaultIndex
        data.activeEndTime = assemble_info[0].etime
        data.goods_id = goods_id
        this.data.shop_goods_id = data.goods_id
        data.assemble_people = assemble_people
        data.goods_name = data.goods_name
        data.goods_original_price = data.goods_original_price
        data.low_goods_price = data.low_goods_price
        data.sku_type = JSON.parse(data.sku_type)
        data.sku = JSON.stringify(data.sku)
        data.activity_id = activity_id
        data.goods_cover = data.goods_cover.split(",");
        data.goods_content = data.goods_content.replace(/\<img/gi, '<img style="width:100%;height:100%;display:block;margin-top:-1px" ')
        //设置ptItem

        // this.data.ptItem.d_time = 

        this.setData({
          data,
          assemble_people: data.assemble_people,
          need_nums,
          lengs: data.assemble_people > 1 ? true : false
        })
        this.intervalTimeList(this, 1000)
      } else {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })

  },
  intervalTimeList(that, time) {
    that.data.intervals = setInterval(function() {
      let list = that.data.assemble_people
      for (let i = 0; i < list.length; i++) {
        let {
          hrStr,
          minStr,
          secStr,
          deviation,
          day,
          shengSR
        } = util.getTimeminus(list[i].d_time)
        // console.log(hrStr,
        //   minStr,
        //   secStr,
        //   deviation,
        //   day,
        //   shengSR,321123321132)
        deviation--;
        var goodsList = 'assemble_people[' + i + '].time';
        this.setData({
          [goodsList]: hrStr + ':' + minStr + ':' + secStr
        });

        if (deviation < 0) {
          assemble_people.splice(i, 1)
          console.log(deviation, assemble_people, 'jsy')
          this.setData({
            [goodsList]: "00" + ':' + "00" + ':' + "00",
            assemble_people
          });
        }
      }
    }.bind(that), time);
  },
  showSpecification() {
    this.setData({
      showSpecification: !this.data.showSpecification,
      parent_id: 0
    })
  },


  jumpVip(e) {
    let card_id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `../Vip/Vip?card_id=${card_id}&index=0&stasus=false`,
    })
  },

  preventTouchMove: function(event) {
    return false
  },

  myevent(e) {
    this.setData({
      showSpecification: e.detail.showSpecification
    })
  },

  onHide: function() {
    clearInterval(this.data.interval);
  },

 
  onUnload: function() {

    clearInterval(this.data.intervals);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  bindchange(e) {
    this.setData({
      current: e.detail.current + 1
    })
  }
}))